<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="content-type" content="no-cache, must-revalidate"/>
    <meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT"/>
    <title>资料信息</title>

    <link rel="stylesheet" href="css/my.css">
    <link href="/front/css/bootstrap.min.css" rel="stylesheet">

    <style>
        .nx-nav-container ul {
            width: 100%;
            height: 100%;
            margin: 0 auto;
            display: flex;
            justify-content: center;
        }

        .nx-nav-container ul li {
            display: inline-block;
            width: 120px;
            text-align: center;
            line-height: 45px;
        }

        .nx-nav-container a {
            color: #FFFFFF;
        }

        .nx-nav-container a:hover {
            color: aquamarine;
            text-decoration: none;
        }

        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body>
<div id="wrapper" v-cloak>
    <div class="nx-header">
        <div style="display: flex;width: 100%; height: 30px; line-height: 30px; background-color: #eee;">
            <div style="flex: 5; padding: 0 10px; color: orangered">
                欢迎访问 欢迎使用本系统
            </div>
            <div v-if="user.name" style="flex: 1">
                欢迎您：{{user.name}}
                <a style="margin-left: 30px; color: blue" href="javascript:void(0)" @click="logout">退出</a>
                <a v-if="isCollect" style="margin-left: 10px" href="collectInfo.html">收藏夹</a>
            </div>
        </div>
    </div>
    <div style="height: 80px; line-height: 80px;">
        <div style="margin-left: 20px; position: relative">
            <img src="img/logo1.png" alt="" style="width: 40px; position: absolute; top: 20px">
            <b style="margin-left:45px;font-size: 25px; text-shadow: 5px 5px 3px #888888;">欢迎使用本系统</b>
        </div>
    </div>

    <div class="nx-nav-container" style="height: 45px; background-color: black">
        <ul>
            <li><a href="index.html">首页</a></li>
            <li><a href="advertiserInfo.html" >公告信息</a></li>
			<li class="active"><a href="fileInfo.html">资料信息</a></li>
			<li><a href="zuoyeInfo.html">提交作业</a></li>

            <li v-if="isShow"><a href="/end/page/index.html" target="_blank">进入后台系统</a></li>
            <li><a href="/end/page/login.html" target="_blank">登录</a></li>
            <li><a href="/end/page/register.html" target="_blank">注册</a></li>
        </ul>
    </div>
    <div class="container"  style="margin-top: 20px">
        <div class="col-md-12">
            <table class="table table-striped table-bordered table-hover">
                <thead>
                <tr style="background-color: #f5f5f5">
                    <th style="width: 50px"></th>
<!--                    <th style="width: 50px">ID</th>-->
					<th>资料名称</th>

                    <th>文件名</th>
                    <th>上传人</th>
                    <th>文件操作</th>
                    <th>详情</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="(data,index) in tableData" :key="data.id">
                    <td><img src="/front/img/file.jpg" alt="" style="width: 30px; height: 30px"></td>
<!--                    <td>{{data.id}}</td>-->
					<td>{{data.name}}</td>

                    <td>{{data.fileName}}</td>
                    <td>{{data.userName}}</td>
                    <td>
                        <button class="btn btn-primary btn-xs" @click="viewModal(data)">预览</button>
                        <button class="btn btn-success btn-xs" @click="downFile(data.id)">下载</button>
                    </td>
                    <td><a :href=`fileInfoDetail.html?id=${data.id}` target="_blank">详情</a></td>
                </tr>
                </tbody>
            </table>
            <div style="text-align: center;">
                <nav aria-label="Page navigation example">
                    <ul class="pagination">
                        <li class="page-item" :class="{ disabled: preActive }">
                            <a class="page-link" href="javascript:void(0)"
                               @click="loadTable(pageInfo.pageNum - 1)">上一页</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="javascript:void(0)" v-if="pageInfo.pageNum > 1" @click="loadTable(pageInfo.pageNum - 1)">{{pageInfo.pageNum - 1}}</a>
                        </li>
                        <li class="page-item disabled">
                            <a class="page-link" aria-disabled="true" href="javascript:void(0)">{{pageInfo.pageNum}}</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="javascript:void(0)" v-if="pageInfo.hasNextPage" @click="loadTable(pageInfo.pageNum + 1)">{{pageInfo.pageNum + 1}}</a>
                        </li>
                        <li class="page-item" :class="{ disabled: nextActive }">
                            <a class="page-link" href="javascript:void(0)" @click="loadTable(pageInfo.hasNextPage? (pageInfo.pageNum + 1) : pageInfo.pageNum)">下一页</a>
                        </li>
                    </ul>
                </nav>
            </div>
        </div>
    </div>
    <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    文件预览
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close" @click="cancel">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" style="height: 300px; text-align: center">
                    <img id="view-img" v-if="imgShow" :src="url" alt="" style="width: 400px;">
                    <video v-if="videoShow" id="video-control" :src="url" controls="controls" style="width: 80%">
                        您的浏览器不支持 video 标签。
                    </video>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="js/jquery-1.10.2.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/vue2.6.11/axios.js"></script>
<script src="js/vue2.6.11/vue.min.js"></script>

<script>
    new Vue({
        el: '#wrapper',
        data: {
            user: {},
            isShow: false,
            imgShow: false,
            videoShow: true,
            url: '',
            tableData: [],
            pageInfo: {},
            preActive: true,
            nextActive: true,
            isCollect: false
        },

        created: function() {
            axios.get('/auth').then(res => {
                if (res.data.code === '0') {
                    this.user = res.data.data;
                    this.isShow = true;
                }
            });
            this.loadTable(1);
        },

        methods: {
            loadTable(pageNum) {
                axios.get("/fileInfo/page/all?pageNum=" + pageNum).then(res => {
                    if (res.data.code === '0') {
                        this.tableData = res.data.data.list;
                        this.pageInfo = res.data.data;
                        this.preActive = !(this.pageInfo.hasPreviousPage);
                        this.nextActive = !(this.pageInfo.hasNextPage);
                    } else {
                        alert(res.data.msg);
                    }
                });
            },
            viewModal: function (data) {
                axios.get("/fileInfo/" + data.id).then(res => {
                    if (res.data.code === '0') {
                        let info = res.data.data;
                        this.url = '/files/download/' + info.fileId;
                        if (/\.(png|jpg|gif|jpeg|webp)$/.test(info.fileName)) {
                            this.imgShow = true;
                            this.videoShow = false;

                            let img = new Image();
                            img.src = this.url;
                            // 加载完成执行
                            img.onload = function(){
                                let scale = img.width / img.height;  // 图片的宽高比
                                let width = 270 * scale;  // 先按照高度等比缩小
                                if(width > 570) {   // 如果缩小后的宽度超出模态框（模态框宽598px），则再次按照宽度等比缩小
                                    let height = 570 / scale;
                                    $('#view-img').css('width', '570px').css('height', height + 'px');
                                } else {
                                    $('#view-img').css('width', width + 'px').css('height', '270px');
                                }
                            };
                        } else if (/\.mp4$/.test(info.fileName)) {
                            this.imgShow = false;
                            this.videoShow = true;
                        } else {
                            alert('不支持该格式的预览');
                            return;
                        }
                        $("#myModal").modal('show');
                    } else {
                        alert(res.data.msg);
                    }
                });
            },
            cancel: function () {
                $("#video-control")[0].pause();
            },
            downFile: function (id) {
                axios.get("/fileInfo/" + id).then(res => {
                    if (res.data.code === '0') {
                        location.href = '/files/download/' + res.data.data.fileId;
                    } else {
                        alert(res.data.msg);
                    }
                });
            },
            logout() {
                axios.get("/logout").then(res => {
                    if(res.data.code === '0') {
                        location.href = '/front/index.html';
                    } else {
                        msg('error', res.data.msg);
                    }
                })
            }
        }
    })
</script>
</body>
</html>
